#include "CSystemNoticeCfg.h"
#include "commerrno.h"
#include "../mainctrl.h"
#include "MsgGM.pb.h"

bool CSystemNoticeCfgData::IsValidSegTime()
{
    for (int i = 0; i < _astSegTime.Size(); ++i)
    {
        if (_astSegTime[i]._iStart < 0 || _astSegTime[i]._iStop <= 0 ||
            _astSegTime[i]._iStop <= _astSegTime[i]._iStart)
        {
            CT_ERROR(("segment time error(start=%d stop=%d)",
                       _astSegTime[i]._iStart,_astSegTime[i]._iStop));
            return false;
        }
        if (i - 1 >= 0 && _astSegTime[i]._iStart <= _astSegTime[i - 1]._iStop)
        {
            CT_ERROR(("segment time error(next start=%d less than last stop=%d)",
                       _astSegTime[i]._iStart,_astSegTime[i - 1]._iStop));
            return false;
        }				
    }

    return true;
}

void CSystemNoticeCfgData::RefreshSystemNotice()
{
    if (!IsValidID())
    {
        CT_ERROR(("system notice config(%d) error!",GetID()));
        return;
    }

    //1.首先检查持续天数
    int iLimitTime = CTTimeUtils::GetDayBeginSec(
            CMainCtrl::m_pstMain->m_stZoneOnlineData._stZoneData._iZoneOpenTime)
            + 86400 * GetLastDay();
    if (CMainCtrl::m_pstMain->m_iNow > iLimitTime)
    {
        return;
    }

    int iDayTime = (CMainCtrl::m_pstMain->m_stTm.tm_sec +
                    CMainCtrl::m_pstMain->m_stTm.tm_min * 60 +
                    CMainCtrl::m_pstMain->m_stTm.tm_hour * 3600);
    int iDayZeroTime = CMainCtrl::m_pstMain->m_iDayZeroTime;

    int iRateVal = 0;
    int iLimitNotice = 0;
    int iInterSec = GetInterval();

    for (int i = 0; i < GetSegTimeSize(); ++i)
    {
        CActSegTime& stSegTime = GetSegTime(i);
        if (iDayTime < stSegTime._iStart)
        {
            break;//低于最小直接退出
        }
        if (iDayTime > stSegTime._iStop)
        {
            continue;//高于较大判断下一个
        }
        if (stSegTime._iStop <= stSegTime._iStart)
        {
            //此情况不合理
            CT_WARNING((""));
            continue;
        }

        iRateVal = (stSegTime._iStop - stSegTime._iStart) / iInterSec;

        for (int j = iRateVal; j >= 0; --j)
        {
            iLimitNotice = stSegTime._iStart + iDayZeroTime + (j+1) * iInterSec;
            if (iLimitNotice > stSegTime._iStop + iDayZeroTime)
            {
                iLimitNotice = stSegTime._iStop + iDayZeroTime;
            }

            if (iDayTime >= stSegTime._iStart + j * iInterSec &&
                GetLastNotice() < iLimitNotice)
            {
                CMainCtrl::m_pstMain->m_stActMgr.AddSysNotice(GetID());
                SetLastNotice(iLimitNotice);
                break;
            }
        }
    }
}

int CSystemNoticeCfg::AddConfig(CSystemNoticeCfgData& stCfgData)
{
    if (!stCfgData.IsValidID())
    {
        return ERROR_SYSTEM_NOTICE_ERROR_CFG;
    }
    
    int iIndex = _stSysNoticeCfgHash.HashFind(stCfgData.GetID());
    if (iIndex >= 0)
    {
        return ERROR_SYSTEM_NOTICE_REPEAT_ID;
    }

    iIndex = _stSysNoticeCfgHash.HashAlloc(stCfgData.GetID());
    if (iIndex < 0)
    {
        return RET_SYSTEM_FAIL;
    }

    _stSysNoticeCfgHash[iIndex] = stCfgData;

    return 0;
}

void CSystemNoticeCfg::RefreshSystemNotice()
{
    if (_stSysNoticeCfgHash.Size(T_SYSNOTICE_CFG_HASH::USED_LIST) <= 0)
    {
        return;
    }

    int iIndex = _stSysNoticeCfgHash.GetHeadIndex();

    while (iIndex >= 0)
    {
        _stSysNoticeCfgHash[iIndex].RefreshSystemNotice();

        iIndex = _stSysNoticeCfgHash.GetNextIndex(iIndex);
    }
}

int CSystemNoticeCfg::GMDealSystemNotice(MsgReqGMPlayer& stReq,
        MsgAnsGMPlayer& stAns)
{
    CTSizeString<MAX_BATTLE_NAME_BUFF> szNameStr;
    int iZoneOpenTime =
            CMainCtrl::m_pstMain->m_stZoneOnlineData._stZoneData._iZoneOpenTime;

    switch (stReq.sub_type())
    {
    case ID_GM_PLAYER_SUB_GET_SYSTEM_NOTICE:
        {
            szNameStr.Cat("大区开服时刻:%d", iZoneOpenTime);

            szNameStr.Cat("系统提示总数:%d",
                _stSysNoticeCfgHash.Size(T_SYSNOTICE_CFG_HASH::USED_LIST));

            int iIndex = _stSysNoticeCfgHash.GetHeadIndex();
            
            while (iIndex >= 0)
            {
                szNameStr.Cat("\nID:%d ",_stSysNoticeCfgHash[iIndex].GetID());
                szNameStr.Cat("间隔:%d ",
                    _stSysNoticeCfgHash[iIndex].GetInterval());
                szNameStr.Cat("持续天数:%d ",
                    _stSysNoticeCfgHash[iIndex].GetLastDay());
                szNameStr.Cat("上次提示:%d ",
                    _stSysNoticeCfgHash[iIndex].GetLastNotice());

                szNameStr.Cat("段:%d",
                    _stSysNoticeCfgHash[iIndex].GetSegTimeSize());

                for (int i = 0;
                        i < _stSysNoticeCfgHash[iIndex].GetSegTimeSize(); ++i)
                {
                    CActSegTime& stSegTime =
                        _stSysNoticeCfgHash[iIndex].GetSegTime(i);

                    szNameStr.Cat(" [%d:%d]",stSegTime._iStart,
                        stSegTime._iStop);
                }

                iIndex = _stSysNoticeCfgHash.GetNextIndex(iIndex);
            }

            stAns.set_name(szNameStr.Get());
        }
        break;
    default:
        return ERROR_MAIN_SUB_CMD;
    }
    return 0;
}

